package com.leo.component;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.component.Component;
import com.almasb.fxgl.texture.AnimatedTexture;
import com.almasb.fxgl.texture.AnimationChannel;
import com.leo.constant.ArcheryType;
import javafx.scene.layout.HBox;
import javafx.util.Duration;

/**
 * @Author: wangss
 * @CreateTime: 2023-06-08  20:31
 * @Description: 分数
 */
public class ScoreComponent extends Component {

    @Override
    public void onAdded() {
        // area
        String area = entity.getString("area");
        int score;
        switch (area) {
            case "high" -> score = 20;
            case "middle" -> score = 10;
            default -> score = 1;
        }

        Entity singleton = FXGL.getGameWorld().getSingleton(ArcheryType.SCORE_SHOW);
        ScoreShowComponent scoreComponent = singleton.getComponent(ScoreShowComponent.class);
        scoreComponent.refreshScore(score);

        String[] value = String.valueOf(score).split("");
        HBox box = new HBox();
        for (String s : value) {
            AnimatedTexture texture = new AnimatedTexture(new AnimationChannel(FXGL.image("score/" + area + ".png"),
                    10, 220 / 10, 28, Duration.seconds(.8), Integer.valueOf(s), Integer.valueOf(s)));
            box.getChildren().add(texture);
        }
        entity.getViewComponent().addChild(box);
    }
}
